package com.shigu.cron;

import com.opentae.data.mall.beans.FlashSales;
import com.opentae.data.mall.examples.FlashSalesExample;
import com.opentae.data.mall.interfaces.FlashSalesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author:         沐风
 * @Descirption     定时减少库存
 * @CreateDate:     2019/8/31 7:40 PM
 */
@Component
public class ModifyNum {

    @Autowired
    FlashSalesMapper flashSalesMapper;

    @Scheduled(cron="0 */30 * * * ?")
    public void modifyFlashNum(){
        List<FlashSales> flashSales = flashSalesMapper.selectByExample(new FlashSalesExample());
        for (int i=0;i<flashSales.size();i++){
            FlashSales flash = flashSales.get(i);
            int number = new java.util.Random().nextInt(1) + 3;//随机减少3-5件
            if((flash.getNumber() - number)<=0){
                continue;
            }
            flashSalesMapper.modifyFlashNum(flash.getFlashId(),number);
        }
    }
}
